package com.winit.openapi.erp.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.airvoyage.AirVoyageService;
import com.winit.erp.spi.airvoyage.command.AirVoyageCommand;
import com.winit.erp.spi.airvoyage.vo.FlightsVoyage;
import com.winit.erp.spi.seavoyage.SeaVoyageService;
import com.winit.erp.spi.seavoyage.command.SeaVoyageCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ups.spi.systemrule.TransportModeService;
import com.winit.ups.spi.systemrule.command.TransportModeCommand;
import com.winit.ups.spi.systemrule.vo.TransportMode;

/**
 * 航次查询控制类
 * 
 * @author adminchenliu
 */
@Controller
@RequestMapping(value = "/erp/Voyage")
public class VoyageController extends BaseController {

    private Logger               logger = LoggerFactory.getLogger(VoyageController.class);

    @Autowired
    private AirVoyageService     airVoyageService;
    @Autowired
    private SeaVoyageService     seaVoyageService;
    @Resource
    private TransportModeService transportModeService;

    @RequestMapping(value = "/queryVoyage")
    public String queryVoyage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            // 判断是JSON是否存在当前KEY
            if (json.containsKey("originalCode")) {
                // 获取参数值
                String code = json.getString("originalCode");
                String warehouseId = json.getString("warehouseId");
                String warehousesourceId = json.getString("warehousesourceId");
                // 判断参数值是否为空
                if (null != code) {
                    TransportModeCommand tCommand = new TransportModeCommand();
                    TransportMode transport = new TransportMode();
                    transport.setTransportModeCode(code);
                    tCommand.setTransportMode(transport);
                    List<TransportMode> tList = transportModeService.getTransportModeList(tCommand);
                    if (tList.size() > 0) {
                        TransportMode transportMode = tList.get(0);
                        tCommand.setTransportMode(transportMode);
                        AirVoyageCommand command = new AirVoyageCommand();
                        command.setDeliveryType(transportMode.getType());
                        command.setWarehouseId(warehouseId);
                        command.setWarehousesourceId(warehousesourceId);
                        List<FlightsVoyage> listAir = airVoyageService.queryAirVoyage(command);
                        logger.info("查询的航空航次有" + listAir.size() + "条数据");
                        if (listAir.size() > 0) {
                            responseMsg.setData(listAir);
                        } else {
                            SeaVoyageCommand commandSea = new SeaVoyageCommand();
                            commandSea.setDeliveryType(transportMode.getType());
                            commandSea.setWarehouseId(warehouseId);
                            commandSea.setWarehousesourceId(warehousesourceId);
                            List<FlightsVoyage> listSea = seaVoyageService.querySeaVoyageByType(commandSea);
                            logger.info("查询的海运航次有" + listSea.size() + "条数据");
                            responseMsg.setData(listSea);
                        }
                    }
                    return ERROR;
                }

            }

        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
}
